***************************************************
*** RUN REGS COMPARING HOSP TO NBRS WITHIN 150M ***
*** Last edited: 7/26/2021         		   	    ***
***************************************************
log using "${CodePath}/log/regressions_150m_$S_DATE.log", text replace
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/Event Studies/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/Event Studies/nearseg100_state_2__state/"
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/Event Studies/nearseg100_state_2__state/CI95/"

*--------------------------------------------------
* Merge data and run regressions
*--------------------------------------------------

local hospvars    "`hospvars' log_n_claims"

use "${DataPath}/within150m/pnlist_groups_150m.dta", clear
duplicates drop hospcomp_group_150 pn, force
expand 9
sort pn
bys pn hospcomp_group_150 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	keep pn fyear dist_to_border jk_state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* `hospvars' nearseg100_state_2 nearseg100_state RACregion state
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_150 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

// create an indicator for being on the "high" side of each comparison group (in the region with the highest audit rate)
	bys hospcomp_group_150 RACregion: egen group_RACregion_auditrate = mean(jk_state_audit_in2011)
	bys hospcomp_group_150: egen max_group_auditrate = max(group_RACregion_auditrate)

	gen 	highRACside = 0
	replace highRACside = 1 if group_RACregion_auditrate == max_group_auditrate

	bys hospcomp_group_150: egen n_RACregion = nvals(RACregion)


foreach hospvar of varlist `hospvars' {
	ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
			,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
	iv_event_study95 audit_in2011
	graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/Event Studies/nearseg100_state_2__state/CI95/`hospvar'_IV.png", replace

	// Standardized scale for log_n_claims
	if "`hospvar'" == "log_n_claims"{
		
		ivreghdfe `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
			 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
						,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)

		iv_event_study95_yscale  audit_in2011 -0.06 0.06 0.03
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_150m/Event Studies/nearseg100_state_2__state/CI95/log_`hospvar'_IV_yscale.png", replace

	}
} // hospvar

log close
